//
//  AreaPickView.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/25.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "AreaPickView.h"
#import "LXProvince.h"
#define plist
@interface AreaPickView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic,strong)NSArray *provinceArray;
@property (nonatomic,assign)NSInteger provinceIndex;
@end
@implementation AreaPickView


-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.delegate = self;
        self.dataSource =  self;
    }
    return self;
}


#pragma mark --<UIPickerViewDataSource>
//一共有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
//每一列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return self.provinceArray.count;
    }else{
#ifdef plist
        LXProvince *p = self.provinceArray[self.provinceIndex];
        return p.cities.count;
#else
        if(self.cityArray.count == 1) return 0;
        return self.cityArray.count;
#endif
        
    }
}
//每一列每一行的名称
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
#ifdef plist
        LXProvince *p = self.provinceArray[row];
        return p.name;
#else
        Province * p = self.provinceArray[row];
        return p.name;
#endif
    }else{
#ifdef plist
        
        LXProvince *p = self.provinceArray[self.provinceIndex];
        return p.cities[row];
#else
        if(row<self.cityArray.count)
        {
            City * c = self.cityArray[row];
            return c.name;
        }
        else
        {
            return @"";
        }
#endif
    }
}

#pragma mark --<UIPickerViewDelegate>
//选中某一行时会调用该方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        self.provinceIndex = row;
#ifdef plist
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];
#else
        Province * p = self.provinceArray[row];
        self.cityArray = [self.shareManager findCityFromprovince:p];
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];
#endif
    }
    
#ifdef plist
    LXProvince *p = self.provinceArray[self.provinceIndex];
    NSArray *cities = p.cities;
    NSInteger citiesIndex = [pickerView selectedRowInComponent:1];
    NSString *citiesName = cities[citiesIndex];
    if(self.PickCallBack){
        self.PickCallBack([NSString stringWithFormat:@"%@%@",p.name,citiesName]);
    }
    
    if(self.pickDelegate&&[self.pickDelegate respondsToSelector:@selector(AreaPickView:didPickProvince:city:)]){
        [self.pickDelegate AreaPickView:self didPickProvince:p.name city:citiesName];
    }
#else
    Province * p = self.provinceArray[self.provinceIndex];
    NSInteger citiesIndex = [pickerView selectedRowInComponent:1];
    City *city = self.cityArray[citiesIndex];
    if(self.cityArray.count != 1)
        self.text = [NSString stringWithFormat:@"%@%@",p.name,city.name];
    else self.text = [NSString stringWithFormat:@"%@",p.name];
#endif
    
}

- (NSArray *)provinceArray
{
    if (_provinceArray == nil) {
#ifdef plist
        NSArray *dictArr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"provinces.plist" ofType:nil]];
        NSMutableArray *modelArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArr) {
            [modelArray addObject:[LXProvince provinceWithDict:dict]];
        }
        _provinceArray = modelArray;
#else
        _provinceArray = [self.shareManager findProvince];
        _cityArray = [self.shareManager findCityFromprovince:_provinceArray.firstObject];
#endif
        
    }
    return _provinceArray;
}
@end
